<?php

namespace app\goods\controller;

use think\Controller;
use think\Request;

class Ask extends GoodsController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($status='')
    {
        $sx = $status?'asc':$status==1?'asc':'desc';
        if($status == '' || $status ==1){
            // 如果未答复按询价时间由早到晚
            $sx = 'asc';
            $fie = 'xjtime';
        }else{
            // 如果已回复按答复时间由晚到早
            $sx = 'desc';
            $fie = 'dftime';
        }
        $data = db('ask')
            ->where('status',$status)
            ->order($fie,$sx)
            ->paginate(10);
//            ->select();
//        dump($data);
//        dump($status);
        return view('index',['data'=>$data,'status'=>$status]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        // 查询询价信息
        $data = db('ask')
            ->where('id',$id)
            ->find();
//        dump($data);

        return view('edit',['data'=>$data]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        $post = input('post.');

        if(empty($post['qiquanlv'])){
            $this->error('期权率不能为0');
        }

        $id = $post['id'];
        $uid = $post['uid'];

        $post['status'] = 2;
        $post['dftime'] = time();

        unset($post['id']);

        $res = db('ask')
            ->where('id',$id)
            ->update($post);
        if($res){
            $data = db('user')
                ->join('user_data','user_data.uid = qq_user.id')
                ->field('id,tel,name')
                ->find($uid);
            // 用户名字
            $name = isset($data['name'])?$data['name']:'用户';

            //发站内信
            $arr = [];
            $arr['msg'] = '尊敬的'.$name.',您的询价已回复,请进入询价中心查看';
            $arr['time'] = time();
            //插入并获取mid
            $mid = db('msg')
                ->insertGetId($arr);
            //发送
            $arr = [];
            $arr['uid'] = $data['id'];
            $arr['mid'] = $mid;
            db('user_msg')
                ->insert($arr);

            //发短信
            $tel = $data['tel'];
            if (preg_match('/^1[345789]\d{9}$/',$tel)){
                require_once EXTEND_PATH.'aliyunSendSMS/Sms.php';
                $response = \SmsDemo::sendSms($tel,$name);
//                echo "发送短信(sendSms)接口返回的结果:\n";
//                print_r($response);
                echo '发送短信'.$response->Message;
            }
            $this->success('回复成功','/goods/Ask/index/status/1',null,1);
        }else{
            $this->error('回复失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = db('ask')
            ->where('id',$id)
            ->delete();

        if($res != 0){
            $arr['status'] = true;
            $arr['msg'] = '删除询价成功';
            return $arr;
        }else{
            $arr['status'] = false;
            $arr['msg'] = '删除询价失败';
            return $arr;
        }
    }

}
